package com.rain.shoppingscienceproj.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.rain.shoppingscienceproj.R;
import com.rain.shoppingscienceproj.module.CarData;
import com.rain.shoppingscienceproj.module.Goods;
import com.rain.shoppingscienceproj.module.MyCarData;
import com.rain.shoppingscienceproj.module.ShoppingcarDto;
import com.rain.shoppingscienceproj.utils.ImageUtils;
import com.rain.shoppingscienceproj.utils.layoutmanager.CustomLinearLayoutManager;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: Rain
 * Create: 2018/9/3 0:07
 */
public class CarAdapter extends RecyclerView.Adapter<CarAdapter.MyViewHolder> {

    private Context mContext;

    private List<CarData> list;

    private CheckItemClickListener checkItemClickListener;

    private LongItemListener longItemListener;

    private CountListenter countListenter;

    DecimalFormat df = new DecimalFormat("#.00");

    private int num = 0;

    public void setCheckItemClickListener(CheckItemClickListener checkItemClickListener) {
        this.checkItemClickListener = checkItemClickListener;
    }

    public void setLongItemListener(LongItemListener longItemListener) {
        this.longItemListener = longItemListener;
    }

    public void setCountListenter(CountListenter countListenter) {
        this.countListenter = countListenter;
    }

    public CarAdapter(Context mContext) {
        this.mContext = mContext;
    }


    public void setList(List<CarData> carDataList) {
        this.list = carDataList;
        notifyDataSetChanged();
    }


    public void allSelect(boolean isSelect) {
        if (isSelect) {
            num = list.size();
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setSelect(true);
            }
        } else {
            num = 0;
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setSelect(false);
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.car_item_make, parent, false);//解决宽度不能铺满
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        CarData myCarData = list.get(position);

        holder.tv_title.setText(myCarData.getShop().getShopName());
        if (myCarData.getShop().getShopImage() != null || !"".equals(myCarData.getShop().getShopImage())) {
            ImageUtils.imageShop(mContext, myCarData.getShop().getShopImage(), holder.iv_store_image);
        }
        double money = 0.00;
        for (ShoppingcarDto goods :
                myCarData.getList()) {
//            goods.setSize(goods.getNum());
            money += Double.valueOf(goods.getGoodsPay());
        }
        List<ShoppingcarDto> goodsList = new ArrayList<>();
        if (myCarData.isSelect()) {
            goodsList.addAll(myCarData.getList());
            holder.iv_check.setImageResource(R.drawable.address_complete);
        } else {
            goodsList.remove(myCarData.getList());
            holder.iv_check.setImageResource(R.drawable.wallet_recharge_checkbox);
        }
        CustomLinearLayoutManager linearLayoutManager = new CustomLinearLayoutManager(mContext);
        holder.rv_goods_order.setLayoutManager(linearLayoutManager);
        CarGoodsAdapter carGoodsAdapter = new CarGoodsAdapter(mContext);
        holder.rv_goods_order.setAdapter(carGoodsAdapter);
        carGoodsAdapter.setList(myCarData.getList());
        carGoodsAdapter.selectAllGoods(myCarData.isSelect());
        holder.iv_check.setOnClickListener(view ->
                {
                    check(myCarData, carGoodsAdapter, holder.iv_check);
                    if (myCarData.isSelect()) {
                        goodsList.addAll(myCarData.getList());
                    } else {
                        goodsList.clear();
                    }
                }
        );
        carGoodsAdapter.setLongItemListener((goods, goodsPosition) -> {
            longItemListener.LongItemLister(myCarData.getShop().getShopId(), goods.getShoppingcarId(), position);
        });

        carGoodsAdapter.setCountListenter((goods, goodsNum, isOperation) -> {
            countListenter.CountListenter(goods.getGoodsPay(), isOperation, goods.isSelect);

        });
        carGoodsAdapter.setCheckItemClickListener((goods, isStoreSelect) -> {
            if (isStoreSelect) {
                myCarData.setSelect(isStoreSelect);
                holder.iv_check.setImageResource(R.drawable.address_complete);
                num++;
            } else {
                if (myCarData.isSelect()) {
                    myCarData.setSelect(isStoreSelect);
                    num--;
                }
                holder.iv_check.setImageResource(R.drawable.wallet_recharge_checkbox);
            }

            if (goods.isSelect) {
                goodsList.add(goods);
            } else {
                goodsList.remove(goods);
            }
            if (num == list.size()) {
                checkItemClickListener.checkItemClickListener(myCarData, true);
            } else {
                checkItemClickListener.checkItemClickListener(myCarData, false);
            }
        });

        holder.tv_total_text.setText("共计" + list.get(position).getList().size() + "件商品  小计:");
        if (money < 1) {
            holder.tv_money.setText(0 + df.format(money));
        } else {
            holder.tv_money.setText(df.format(money));
        }

    }

    private void check(CarData myCarData, CarGoodsAdapter carGoodsAdapter, ImageView iv_check) {
        List<ShoppingcarDto> goodsList = new ArrayList<>();
        if (myCarData.isSelect()) {
            myCarData.setSelect(false);
            goodsList.removeAll(myCarData.getList());
            iv_check.setImageResource(R.drawable.wallet_recharge_checkbox);
            num--;
        } else {
            myCarData.setSelect(true);
            goodsList.addAll(myCarData.getList());
            iv_check.setImageResource(R.drawable.address_complete);
            num++;
        }
        if (num == list.size()) {
            checkItemClickListener.checkItemClickListener(myCarData, true);
        } else {
            checkItemClickListener.checkItemClickListener(myCarData, false);
        }
        carGoodsAdapter.selectAllGoods(myCarData.isSelect());

    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        } else {
            return list.size();
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_check)
        ImageView iv_check;

        @BindView(R.id.iv_store_image)
        ImageView iv_store_image;

        @BindView(R.id.tv_title)
        TextView tv_title;

        @BindView(R.id.tv_total_text)
        TextView tv_total_text;

        @BindView(R.id.tv_money)
        TextView tv_money;

        @BindView(R.id.rv_goods_order)
        RecyclerView rv_goods_order;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public interface CheckItemClickListener {
        void checkItemClickListener(CarData myCarData, boolean isAllSelect);
    }


    public interface CountListenter {
        void CountListenter(int money, boolean isOperation, boolean isRole);
    }

    public interface ItemClickListener {
        void ItemClickListener(String shopId);
    }

    public interface LongItemListener {
        void LongItemLister(String shopId, String goodsId, int position);
    }
}
